package com.ruoyi.common.utils.obs;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.ObjectMetadata;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import com.ruoyi.common.config.ObsConfig;
import com.ruoyi.common.core.vo.ObsUploadRespVo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 华为云存储工具类
 * @author Kun
 */
@Slf4j
@Component
public class ObsUtils {

    @Autowired
    private ObsConfig obsConfig;
    private ObsClient obsClient;

    /** 文件类型 */
    private final List<String> fileTypes = Arrays.asList("image/png","image/jpeg","Video/x-mpg");

    @PostConstruct
    public void init() {
        obsClient = new ObsClient(obsConfig.getAk(), obsConfig.getSk(), obsConfig.getEndPoint());
    }

    /**
     * 流上传，上传内容大小不能超过5GB。
     * @param file
     * @return
     * @throws IOException
     */
    public ObsUploadRespVo uploadMultipartFile(MultipartFile file) throws IOException {
        String uuidName = UUID.randomUUID().toString().replaceAll("-", "");
        ObjectMetadata metadata = new ObjectMetadata();
        String suffixName;
        if("blob".equals(file.getOriginalFilename())){
             suffixName = FileUploadUtils.getExtension(file);
        }else{
             suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        }
        String contentType = file.getContentType();
        String fileName = uuidName+suffixName;
        String objectKey = "";
        switch (contentType) {
            case "image/png":
            case "image/jpeg":
                objectKey = "image/" + fileName;
                break;
            case "Video/x-mpg":
                objectKey = "video/" + fileName;
                break;
            default: objectKey =  fileName;
                break;
        }
        metadata.setContentType(contentType);
        PutObjectResult result = obsClient.putObject(obsConfig.getBucketName(), objectKey, file.getInputStream(), metadata);
        ObsUploadRespVo vo = new ObsUploadRespVo();
        vo.setBucketName(result.getBucketName());
        vo.setOriginalName(uuidName + suffixName);
        vo.setObjectKey(result.getObjectKey());
        vo.setObjectUrl(result.getObjectUrl());
        return vo;
    }

    /**
     * 下载文件
     * @param objectKey
     * @return
     */
    public InputStream downFile(String objectKey) {
        ObsObject obsObject = obsClient.getObject(obsConfig.getBucketName(), objectKey);
        return obsObject.getObjectContent();
    }

    /**
     * 文件删除
     * @param objectKey
     */
    public  void deleteFile(String objectKey) {
        obsClient.deleteObject(obsConfig.getBucketName(), objectKey);
    }

    /**
     * 获取对象信息
     * @param objectKey
     * @return
     */
    public ObsObject getObject(String objectKey) {
        try {
            return obsClient.getObject(obsConfig.getBucketName(), objectKey);
        } catch (ObsException e) {
            log.error("objectKey：{},HTTP Code:{},Error Code:{},Request ID:{}", objectKey, e.getResponseCode(), e.getErrorCode(), e.getErrorRequestId());
            return null;
        }
    }
}
